<template>
  <van-icon
    :color="likeCount ? '#ffa500' : '#777'"
    :name="likeCount ? 'good-job' : 'good-job-o'"
    @click="onLike"
  />
</template>

<script>
import { addLike, deleteLike } from "@/api/articles";
export default {
  name: "LikeArticle",
  props: {
    likeCount: {
      type: Number,
      required: true,
    },
    id: {
      type: [Number, String],
      required: true,
    },
  },
  methods: {
    async onLike() {
      try {
        this.$toast.loading({
          duration: 0,
          message: "加载中...",
          forbidClick: true,
        });
        if (this.likeCount) {
          await deleteLike(this.id);
        } else {
          await addLike(this.id);
        }
        this.$emit("update:like-count", this.likeCount ? 0 : 1);
      } finally {
        this.$toast.clear();
      }
    },
  },
};
</script>